package com.wetask.admin.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.wetask.dao.UserDao;
import com.wetask.utils.DataHelper;

public class UserDetailServiceImpl implements UserDetailsService{
	
	@Autowired
	private UserDao userDao;
	
	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		List<GrantedAuthority> AUTHORITIES = new ArrayList<GrantedAuthority>();
		com.wetask.domain.model.User user = userDao.getUserByEmail(username, DataHelper.UserRole.ROLE_FOR_CLIENT.getCode());
		if (user != null) {
			AUTHORITIES.add(new SimpleGrantedAuthority(DataHelper.UserRole.ROLE_FOR_CLIENT.getCode()));
			return new User(username, user.getPassword(), true,
					true, true, true, AUTHORITIES);
		} else{
			throw new UsernameNotFoundException("User is authozization!!");
		}
	}


}
